<?php

/**
 * 导出Excel文件
 * ==================================================================
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/lib_goods.php');

require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
$PHPExcel = new PHPExcel();

/*------------------------------------------------------ */
//-- 订单对账导出Excel文件
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'order_export')
{
    /*获取查询参数值*/
	$  = $_REQUEST[''];
	$  = $_REQUEST[''];
	$  = $_REQUEST[''];
	$  = $_REQUEST[''];
	$  = $_REQUEST[''];
	
	/*查询数据*/
	$order_sn_list = get_order_export();
	
	if (empty($order_sn_list))
    {
        sys_msg("查询数据为空");
		exit;
    }
	
	/*数据导出Excel文件*/
	   $html = '';
	
        //设置excel属性基本信息
        $PHPExcel->getProperties()->setCreator("Neo")
        ->setLastModifiedBy("Neo")
        ->setTitle("东莞XX系统有限公司")
        ->setSubject("订单列表")
        ->setDescription("")
        ->setKeywords("订单列表")
        ->setCategory("");
        $PHPExcel->setActiveSheetIndex(0);
        $PHPExcel->getActiveSheet()->setTitle("订单列表");
      /*   //填入表头主标题
        $PHPExcel->getActiveSheet()->setCellValue('A1', $_CFG['shop_name'].'订单列表');
        //填入表头副标题
        $PHPExcel->getActiveSheet()->setCellValue('A2', '操作者：'.$_SESSION['admin_name'].' 导出日期：'.date('Y-m-d',time()).' 地址：'.$_CFG['shop_address'].' 电话：'.$_CFG['service_phone']);
        //合并表头单元格
        $PHPExcel->getActiveSheet()->mergeCells('A1:Y1');
        $PHPExcel->getActiveSheet()->mergeCells('A2:Y2'); */
        
        //设置表头行高
       /*  $PHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
        $PHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(20); */
        $PHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(30);
        
        //设置表头字体
      /*   $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
        $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $PHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $PHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
        $PHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14); */
		
        $PHPExcel->getActiveSheet()->getStyle('A1:Y1')->getFont()->setBold(true);
 
        //设置单元格边框
        $styleArray = array(  
            'borders' => array(  
                'allborders' => array(  
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的  
                    'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框  
                    //'color' => array('argb' => 'FFFF0000'),  
                ),  
            ),  
        );
        
        //表格宽度
        $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);//订单编号
        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);//下单时间
        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);//下单人
        $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);//商品编号
        $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);//商品单价
        $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);//购买数量
        $PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);//市场价
        $PHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);//实际售价
        $PHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);//额外折扣
        $PHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(35);//配送费用
        $PHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);//使用余额
        $PHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15);//使用积分
        $PHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(25);//使用红包
        $PHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(15);//实际付款金额
        $PHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(25);//付款时间
        $PHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);//支付方式
        $PHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(15);//订单状态
        $PHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(25);//订单完成时间
        $PHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(15);//商品供应商
        $PHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(15);//商品供应价
	
		$PHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(15);//商品数量
		$PHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(15);//采购单价
		$PHPExcel->getActiveSheet()->getColumnDimension('W')->setWidth(15);//商品采购总价
		$PHPExcel->getActiveSheet()->getColumnDimension('X')->setWidth(15);//供应商发货时间
		$PHPExcel->getActiveSheet()->getColumnDimension('Y')->setWidth(15);
		//货款支付时间

		
        //表格标题
        $PHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '下单时间');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '下单人');
        $PHPExcel->getActiveSheet()->setCellValue('D1', '商品编号');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '商品单价');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '购买数量');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '市场价');
        $PHPExcel->getActiveSheet()->setCellValue('H1', '实际售价');
        $PHPExcel->getActiveSheet()->setCellValue('I1', '额外折扣');
        $PHPExcel->getActiveSheet()->setCellValue('J1', '配送费用');
        $PHPExcel->getActiveSheet()->setCellValue('K1', '使用余额');
        $PHPExcel->getActiveSheet()->setCellValue('L1', '使用积分');
        $PHPExcel->getActiveSheet()->setCellValue('M1', '使用红包');
        $PHPExcel->getActiveSheet()->setCellValue('N1', '实际付款金额');
        $PHPExcel->getActiveSheet()->setCellValue('O1', '付款时间');
        $PHPExcel->getActiveSheet()->setCellValue('P1', '支付方式');
        $PHPExcel->getActiveSheet()->setCellValue('Q1', '订单状态');
        $PHPExcel->getActiveSheet()->setCellValue('R1', '订单完成时间');
        $PHPExcel->getActiveSheet()->setCellValue('S1', '商品供应商');
        $PHPExcel->getActiveSheet()->setCellValue('T1', '商品供应价');
		$PHPExcel->getActiveSheet()->setCellValue('U1', '商品数量');
		$PHPExcel->getActiveSheet()->setCellValue('V1', '采购单价');
		$PHPExcel->getActiveSheet()->setCellValue('W1', '商品采购总价');
		$PHPExcel->getActiveSheet()->setCellValue('X1', '供应商发货时间');
		$PHPExcel->getActiveSheet()->setCellValue('Y1', '货款支付时间');
 
        $hang = 2; 
        foreach ($order_sn_list as $order_sn) {
            /* 取得订单信息 */
            $order = order_info(0, $order_sn);
            if (empty($order)) {
                continue;
            }
 
            /* 根据订单是否完成检查权限 */
            if (order_finished($order)) {
                if (!admin_priv('order_view_finished', '', false)) {
                    continue;
                }
            } else {
                if (!admin_priv('order_view', '', false)) {
                    continue;
                }
            }
 
            /* 如果管理员属于某个办事处，检查该订单是否也属于这个办事处 */
            $sql       = "SELECT agency_id FROM " . $ecs->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'";
            $agency_id = $db->getOne($sql);
            if ($agency_id > 0) {
                if ($order['agency_id'] != $agency_id) {
                    continue;
                }
            }
 
            /* 取得用户名 */
            if ($order['user_id'] > 0) {
                $user = user_info($order['user_id']);
                if (!empty($user)) {
                    $order['user_name'] = $user['user_name'];
 
                }
            }
 
            /* 取得区域名 */
            $sql             = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " . "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " . "FROM " . $ecs->table('order_info') . " AS o " . "LEFT JOIN " . $ecs->table('region') . " AS c ON o.country = c.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS p ON o.province = p.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS t ON o.city = t.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS d ON o.district = d.region_id " . "WHERE o.order_id = '$order[order_id]'";
            $order['region'] = $db->getOne($sql);
            
            /* 其他处理 */
            $order['order_time']    = local_date($_CFG['time_format'], $order['add_time']);
			/* 新增 */
			
			 $order['confirm_time']      = $order['confirm_time'] > 0 ? local_date($_CFG['time_format'], $order['confirm_time']) : $_LANG['ps'][PS_UNPAYED];
			
			
            $order['pay_time']      = $order['pay_time'] > 0 ? local_date($_CFG['time_format'], $order['pay_time']) : $_LANG['ps'][PS_UNPAYED];
			
            $order['shipping_time'] = $order['shipping_time'] > 0 ? local_date($_CFG['time_format'], $order['shipping_time']) : $_LANG['ss'][SS_UNSHIPPED];
			
            $order['status']        = $_LANG['os'][$order['order_status']] . ',' . $_LANG['ps'][$order['pay_status']] . ',' . $_LANG['ss'][$order['shipping_status']];
            $order['invoice_no']    = $order['shipping_status'] == SS_UNSHIPPED || $order['shipping_status'] == SS_PREPARING ? $_LANG['ss'][SS_UNSHIPPED] : $order['invoice_no'];
 
            /* 此订单的发货备注(此订单的最后一条操作记录) */
            $sql = "SELECT action_note FROM " . $ecs->table('order_action') . " WHERE order_id = '$order[order_id]' AND shipping_status = 1 ORDER BY log_time DESC";
            $order['invoice_note'] = $db->getOne($sql);

            $shuliang = 0;
 
            /* 取得订单商品 */
            $sql        = "SELECT o.*, g.goods_number AS storage, o.goods_attr, IFNULL(b.brand_name, '') AS brand_name " . "FROM " . $ecs->table('order_goods') . " AS o " . "LEFT JOIN " . $ecs->table('goods') . " AS g ON o.goods_id = g.goods_id " . "LEFT JOIN " . $ecs->table('brand') . " AS b ON g.brand_id = b.brand_id " . "WHERE o.order_id = '$order[order_id]' ";
            $res        = $db->query($sql);
            $shuliang   = 0;
            $chanpin    = $hang;
            while ($row = $db->fetchRow($res)) {
                $shuliang = $shuliang + 1;
                /* 虚拟商品支持 */
                if ($row['is_real'] == 0) {
                    /* 取得语言项 */
                    $filename = ROOT_PATH . 'plugins/' . $row['extension_code'] . '/languages/common_' . $_CFG['lang'] . '.php';
                    if (file_exists($filename)) {
                        include_once($filename);
                        if (!empty($_LANG[$row['extension_code'] . '_link'])) {
                            $row['goods_name'] = $row['goods_name'] . sprintf($_LANG[$row['extension_code'] . '_link'], $row['goods_id'], $order['order_sn']);
                        }
                    }
                }
 
                $row['formated_subtotal']    = price_format($row['goods_price'] * $row['goods_number']);
                $row['formated_goods_price'] = price_format($row['goods_price']);
                
                //var_dump($order);die;
                //输出订单的商品，由于可能一个人购买多个商品，所以在这先输出了
                $PHPExcel->getActiveSheet()->setCellValue('D' . $chanpin, $row['goods_sn']);
                $PHPExcel->getActiveSheet()->setCellValue('E' . $chanpin, $row['shop_price']);
                $PHPExcel->getActiveSheet()->setCellValue('F' . $chanpin, $row['goods_number']);
                $PHPExcel->getActiveSheet()->setCellValue('G' . $chanpin, $row['market_price']);
                $PHPExcel->getActiveSheet()->setCellValue('H' . $chanpin, $row['goods_price']);
                $PHPExcel->getActiveSheet()->setCellValue('S' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('T' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('U' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('V' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('W' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('X' . $chanpin, $row['']);
				$PHPExcel->getActiveSheet()->setCellValue('Y' . $chanpin, $row['']);
				
				
				
                $chanpin      = $chanpin + 1;
            }

            for ($kk = $hang; $kk < ($hang + $shuliang); $kk++) {
                //合并单元格
                $PHPExcel->getActiveSheet()->mergeCells('A' . $hang . ':A' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('B' . $hang . ':B' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('C' . $hang . ':C' . $kk);
             
                $PHPExcel->getActiveSheet()->mergeCells('I' . $hang . ':I' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('J' . $hang . ':J' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('K' . $hang . ':K' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('L' . $hang . ':L' . $kk);
                $PHPExcel->getActiveSheet()->mergeCells('M' . $hang . ':M' . $kk);
              
				/* 新增 */
				$PHPExcel->getActiveSheet()->mergeCells('N' . $hang . ':N' . $kk);
				$PHPExcel->getActiveSheet()->mergeCells('O' . $hang . ':O' . $kk);
				$PHPExcel->getActiveSheet()->mergeCells('P' . $hang . ':P' . $kk);
				$PHPExcel->getActiveSheet()->mergeCells('Q' . $hang . ':Q' . $kk);
				$PHPExcel->getActiveSheet()->mergeCells('R' . $hang . ':R' . $kk);

				
				
            }
            $PHPExcel->getActiveSheet()->setCellValue('A' . ($hang), $order['order_sn']." ");//加个空格，防止时间戳被转换
            $PHPExcel->getActiveSheet()->setCellValue('B' . ($hang), $order['order_time']);
            $PHPExcel->getActiveSheet()->setCellValue('C' . ($hang), $order['user_name']);
            $PHPExcel->getActiveSheet()->setCellValue('I' . ($hang), $order['discount']);
            $PHPExcel->getActiveSheet()->setCellValue('J' . ($hang), $order['shipping_fee']);
            $PHPExcel->getActiveSheet()->setCellValue('K' . ($hang), $order['surplus'.'元']);
            $PHPExcel->getActiveSheet()->setCellValue('L' . ($hang), $order['integral_money']);
            $PHPExcel->getActiveSheet()->setCellValue('M' . ($hang), $order['bonus'].'元');
            $PHPExcel->getActiveSheet()->setCellValue('N' . ($hang), $order['order_amount'].'元');
			
            $PHPExcel->getActiveSheet()->setCellValue('O' . ($hang),$order['pay_time']);
            $PHPExcel->getActiveSheet()->setCellValue('P' . ($hang), $order['pay_name']);
            $PHPExcel->getActiveSheet()->setCellValue('Q' . ($hang), $order['order_status']);
            $PHPExcel->getActiveSheet()->setCellValue('R' . ($hang), $order['affirm_time']);
            $PHPExcel->getActiveSheet()->setCellValue('X' . ($hang), $order['']);
			$PHPExcel->getActiveSheet()->setCellValue('Y' . ($hang), $order['']);
			

            
            $hang = $hang + $shuliang;
        }
        //设置单元格边框
        $PHPExcel->getActiveSheet()->getStyle('A1:Y'.$hang)->applyFromArray($styleArray);
        //设置自动换行
        $PHPExcel->getActiveSheet()->getStyle('A4:Y'.$hang)->getAlignment()->setWrapText(true);
        //设置字体大小
        $PHPExcel->getActiveSheet()->getStyle('A4:Y'.$hang)->getFont()->setSize(12);
        //垂直居中
        $PHPExcel->getActiveSheet()->getStyle('A1:Y'.$hang)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //水平居中
        $PHPExcel->getActiveSheet()->getStyle('A1:Y'.$hang)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $Writer = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
        $Writer->save(str_replace('.php', '.xls', __FILE__));
        $url = "order.xls";
        ecs_header("Location: $url\n");

        exit;
	
	
	
	
	
	
}
/*------------------------------------------------------ */
//-- 退货对账
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'list')
{
   //退货对账
}




/**
* 订单对账数据查询
*  order_sn  订单号
*  suppliers_id  
*
*
*
*/

function get_order_export($order_sn = 0,$suppliers_id){
		$ret = array();
	
		/*数据处理*/
		
		
	
        /* 过滤信息 */
		$filter['suppliers_id'] = $_REQUEST['suppliers_id'];
        $filter['order_sn'] = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['consignee'] = json_str_iconv($_REQUEST['consignee']);
            //$_REQUEST['address'] = json_str_iconv($_REQUEST['address']);
        }
        $filter['consignee'] = empty($_REQUEST['consignee']) ? '' : trim($_REQUEST['consignee']);
        $filter['email'] = empty($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
        $filter['address'] = empty($_REQUEST['address']) ? '' : trim($_REQUEST['address']);
        $filter['zipcode'] = empty($_REQUEST['zipcode']) ? '' : trim($_REQUEST['zipcode']);
        $filter['tel'] = empty($_REQUEST['tel']) ? '' : trim($_REQUEST['tel']);
        $filter['mobile'] = empty($_REQUEST['mobile']) ? 0 : intval($_REQUEST['mobile']);
        $filter['country'] = empty($_REQUEST['country']) ? 0 : intval($_REQUEST['country']);
        $filter['province'] = empty($_REQUEST['province']) ? 0 : intval($_REQUEST['province']);
        $filter['city'] = empty($_REQUEST['city']) ? 0 : intval($_REQUEST['city']);
        $filter['district'] = empty($_REQUEST['district']) ? 0 : intval($_REQUEST['district']);
        $filter['shipping_id'] = empty($_REQUEST['shipping_id']) ? 0 : intval($_REQUEST['shipping_id']);
        $filter['pay_id'] = empty($_REQUEST['pay_id']) ? 0 : intval($_REQUEST['pay_id']);
        $filter['order_status'] = isset($_REQUEST['order_status']) ? intval($_REQUEST['order_status']) : -1;
        $filter['shipping_status'] = isset($_REQUEST['shipping_status']) ? intval($_REQUEST['shipping_status']) : -1;
        $filter['pay_status'] = isset($_REQUEST['pay_status']) ? intval($_REQUEST['pay_status']) : -1;
        $filter['user_id'] = empty($_REQUEST['user_id']) ? 0 : intval($_REQUEST['user_id']);
        $filter['user_name'] = empty($_REQUEST['user_name']) ? '' : trim($_REQUEST['user_name']);
        $filter['composite_status'] = isset($_REQUEST['composite_status']) ? intval($_REQUEST['composite_status']) : -1;
        $filter['group_buy_id'] = isset($_REQUEST['group_buy_id']) ? intval($_REQUEST['group_buy_id']) : 0;

        $filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'add_time' : trim($_REQUEST['sort_by']);
		
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $filter['start_time'] = empty($_REQUEST['start_time']) ? '' : (strpos($_REQUEST['start_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_time']) : $_REQUEST['start_time']);
		
        $filter['end_time'] = empty($_REQUEST['end_time']) ? '' : (strpos($_REQUEST['end_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_time']) : $_REQUEST['end_time']);

       /* Y-新增 6.28 付款时间 */

        $filter['start_time_a'] = empty($_REQUEST['start_time_a']) ? '' : (strpos($_REQUEST['start_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['start_time_a']) : $_REQUEST['start_time_a']);
		
        $filter['end_time_a'] = empty($_REQUEST['end_time_a']) ? '' : (strpos($_REQUEST['end_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['end_time_a']) : $_REQUEST['end_time_a']);
// echo($filter['end_time_a']);
// die('123');

       /* Y-新增 6.28 供应商发货时间 */

        /* $filter['start_time_a'] = empty($_REQUEST['start_time_a']) ? '' : (strpos($_REQUEST['start_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['start_time_a']) : $_REQUEST['start_time_a']);
        
         $filter['end_time_a'] = empty($_REQUEST['end_time_a']) ? '' : (strpos($_REQUEST['end_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['end_time_a']) : $_REQUEST['end_time_a']);*/

        /* Y-新增 6.28 支付货款时间 */

        /* $filter['start_time_a'] = empty($_REQUEST['start_time_a']) ? '' : (strpos($_REQUEST['start_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['start_time_a']) : $_REQUEST['start_time_a']);
        
         $filter['end_time_a'] = empty($_REQUEST['end_time_a']) ? '' : (strpos($_REQUEST['end_time_a'], '-') > 0 ?  local_strtotime($_REQUEST['end_time_a']) : $_REQUEST['end_time_a']);*/

        /* Y-新增 6.28 订单完成时间 */

         $filter['start_time_d'] = empty($_REQUEST['start_time_d']) ? '' : (strpos($_REQUEST['start_time_d'], '-') > 0 ?  local_strtotime($_REQUEST['start_time_d']) : $_REQUEST['start_time_d']);
        
         $filter['end_time_d'] = empty($_REQUEST['end_time_d']) ? '' : (strpos($_REQUEST['end_time_d'], '-') > 0 ?  local_strtotime($_REQUEST['end_time_d']) : $_REQUEST['end_time_d']);


        $where = 'WHERE 1 ';

        if ($filter['order_sn'])
        { 
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
		
		if ($filter['suppliers_id']!=null)
        {
			$where .= " AND g.suppliers_id = '$filter[suppliers_id]'";
        }
        
        if ($filter['consignee'])
        {
            $where .= " AND o.consignee LIKE '%" . mysql_like_quote($filter['consignee']) . "%'";
        }
        if ($filter['email'])
        {
            $where .= " AND o.email LIKE '%" . mysql_like_quote($filter['email']) . "%'";
        }
        if ($filter['address'])
        {
            $where .= " AND o.address LIKE '%" . mysql_like_quote($filter['address']) . "%'";
        }
        if ($filter['zipcode'])
        {
            $where .= " AND o.zipcode LIKE '%" . mysql_like_quote($filter['zipcode']) . "%'";
        }
        if ($filter['tel'])
        {
            $where .= " AND o.tel LIKE '%" . mysql_like_quote($filter['tel']) . "%'";
        }
        if ($filter['mobile'])
        {
            $where .= " AND o.mobile LIKE '%" .mysql_like_quote($filter['mobile']) . "%'";
        }
        if ($filter['country'])
        {
            $where .= " AND o.country = '$filter[country]'";
        }
        if ($filter['province'])
        {
            $where .= " AND o.province = '$filter[province]'";
        }
        if ($filter['city'])
        {
            $where .= " AND o.city = '$filter[city]'";
        }
        if ($filter['district'])
        {
            $where .= " AND o.district = '$filter[district]'";
        }
        if ($filter['shipping_id'])
        {
            $where .= " AND o.shipping_id  = '$filter[shipping_id]'";
        }
        if ($filter['pay_id'])
        {
            $where .= " AND o.pay_id  = '$filter[pay_id]'";
        }
        if ($filter['order_status'] != -1)
        {
            $where .= " AND o.order_status  = '$filter[order_status]'";
        }
        if ($filter['shipping_status'] != -1)
        {
            $where .= " AND o.shipping_status = '$filter[shipping_status]'";
        }
        if ($filter['pay_status'] != -1)
        {
            $where .= " AND o.pay_status = '$filter[pay_status]'";
        }
        if ($filter['user_id'])
        {
            $where .= " AND o.user_id = '$filter[user_id]'";
        }
        if ($filter['user_name'])
        {
            $where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['user_name']) . "%'";
        }
        if ($filter['start_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_time]'";
        }
        if ($filter['end_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_time]'";
        }
		
		/* Y-新增 付款时间 条件判断*/
		
		
		 if ($filter['start_time_a'])
        {
            $where .= " AND o.pay_time >= '$filter[start_time_a]'";
        }
        if ($filter['end_time_a'])
        {
            $where .= " AND o.pay_time <= '$filter[end_time_a]'";
        }
		/* Y-新增 供应商发货时间 条件判断*/
		
		 // if ($filter['start_time_b'])
   //      {
   //          $where .= " AND o.pay_time >= '$filter[start_time_c]'";
   //      }
   //      if ($filter['end_time_b'])
   //      {
   //          $where .= " AND o.pay_time <= '$filter[end_time_c]'";
   //      }
		
		
		/* Y-新增 支付货款时间 条件判断*/
		
		 // if ($filter['start_time_c'])
   //      {
   //          $where .= " AND o.pay_time >= '$filter[start_time_c]'";
   //      }
   //      if ($filter['end_time_c'])
   //      {
   //          $where .= " AND o.pay_time <= '$filter[end_time_c]'";
   //      }
		
		/* Y-新增 订单完成时间 条件判断*/
	
		 if ($filter['start_time_d'])
        {
            $where .= " AND o.affirm_time >= '$filter[start_time_d]'";
        }
        if ($filter['end_time_d'])
        {
            $where .= " AND o.affirm_time <= '$filter[end_time_d]'";
        }
	
		
		
		
		
		
		

        //综合状态
        switch($filter['composite_status'])
        {
            case CS_AWAIT_PAY :
                $where .= order_query_sql('await_pay');
                break;

            case CS_AWAIT_SHIP :
                $where .= order_query_sql('await_ship');
                break;

            case CS_FINISHED :
                $where .= order_query_sql('finished');
                break;

            case PS_PAYING :
                if ($filter['composite_status'] != -1)
                {
                    $where .= " AND o.pay_status = '$filter[composite_status]' ";
                }
                break;
            case OS_SHIPPED_PART :
                if ($filter['composite_status'] != -1)
                {
                    $where .= " AND o.shipping_status  = '$filter[composite_status]'-2 ";
                }
                break;
            default:
                if ($filter['composite_status'] != -1)
                {
                    $where .= " AND o.order_status = '$filter[composite_status]' ";
                }
        }

        /* 团购订单 */
        if ($filter['group_buy_id'])
        {
            $where .= " AND o.extension_code = 'group_buy' AND o.extension_id = '$filter[group_buy_id]' ";
        }

        /* 如果管理员属于某个办事处，只列出这个办事处管辖的订单 */
        $sql = "SELECT agency_id FROM " . $GLOBALS['ecs']->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'";
        $agency_id = $GLOBALS['db']->getOne($sql);
        if ($agency_id > 0)
        {
            $where .= " AND o.agency_id = '$agency_id' ";
        }

        /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);

        if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
        {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        }
        elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
        {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        }
        else
        {
            $filter['page_size'] = 15;
        }
	    $join_goods = " LEFT JOIN " .$GLOBALS['ecs']->table('order_goods'). " AS og ON o.order_id=og.order_id LEFT JOIN " .$GLOBALS['ecs']->table('goods'). " AS g ON g.goods_id = og.goods_id ";
        /* 记录总数 */
        if ($filter['user_name'])
        {
            $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('order_info') . " AS o ,".
                   $GLOBALS['ecs']->table('users') . " AS u " . $join_goods. $where;
        }
        else
        {
            $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('order_info') . " AS o ". $join_goods. $where;
        }

        $filter['record_count']   = $GLOBALS['db']->getOne($sql);
        $filter['page_count']     = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;
        //echo $where;//die();
     
	 /* 查询 */
        $sql = "SELECT o.order_id, g.suppliers_id, g.goods_jhprice,o.order_sn, o.add_time, o.order_status, o.shipping_status, o.order_amount, o.money_paid," .
                    "o.pay_status, o.consignee, o.address, o.email, o.tel, o.extension_code, o.extension_id, " .
                    "(" . order_amount_field('o.') . ") AS total_fee, " .
                    "IFNULL(u.user_name, '" .$GLOBALS['_LANG']['anonymous']. "') AS buyer ".
                " FROM " . $GLOBALS['ecs']->table('order_info') . " AS o " .
                " LEFT JOIN " .$GLOBALS['ecs']->table('users'). " AS u ON u.user_id=o.user_id ". $join_goods. $where .
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]";
	
	/*数据查询*/
	
	
	
	
	/*查询结果处理*/
	
	return $ret;
	
	
	
}








?>